/*
pcase.cpp : Defines the entry point for the application.
*/

#include <kpad.h>

typedef struct _MATRIX{
    U8	width;
    U8	height;
    U8	radio;
    U8	reserved;
    U16	y;
    U16	x[5];
}MATRIX;

static TCHAR	wclass[]="KPAD";
static MATRIX	matrix[]={
    22,18,5,1,0,{1,41,81,121,0},
    22,18,5,1,30,{1,41,81,121,0},
    22,18,5,1,60,{1,41,81,121,0},
    22,18,5,1,90,{1,41,81,121,0},
    22,18,5,1,120,{1,41,81,121,0},
    22,18,5,1,150,{1,41,81,121,0},
    22,18,5,1,180,{1,41,81,121,0},
    22,18,5,1,210,{1,41,81,121,0},
    22,18,5,1,240,{1,41,81,121,0},
    22,18,5,1,270,{1,41,81,121,0},
    22,18,5,1,300,{1,41,81,121,0},
    22,18,5,1,330,{1,41,81,121,0},
    0
};
static POINT	points[]={
    {0,0},{0,1},{0,2},{0,3},{0,4},{0,5},{0,6},
    {1,0},{1,1},{1,2},{1,3},{1,4},{1,5},{1,6},
    {2,0},{2,1},{2,2},{2,3},{2,4},{2,5},{2,6},
    {3,0},{3,1},{3,2},{3,3},{3,4},{3,5},{3,6},
    {4,0},{4,1},{4,2},{4,3},{4,4},{4,5},{4,6},
    {5,0},{5,1},{5,2},{5,3},{5,4},{5,5},{5,6},
    {6,0},{6,1},{6,2},{6,3},{6,4},{6,5},{6,6}
};

static LRESULT WmPaint(HWND hwnd,WPARAM wparam,LPARAM lparam){
    HDC		dc;
    PAINTSTRUCT ps;
    //RECT	rc;
    TCHAR	s[300]="KPad";
    U16		*px;
    MATRIX	*py;
    UINT	h,r,w,x,y;

    dc=BeginPaint(hwnd,&ps);
    //GetClientRect(hwnd,&rc);
    for(py=matrix;py->reserved;py++){
	r=py->radio;
	w=py->width;
	h=py->height;
	y=py->y;
	for(px=py->x;x=*px;px++)
	    RoundRect(dc,x,y,x+w,y+h,r,r);
    }
    EndPaint(hwnd,&ps);
    return 0;
}

static LRESULT WmSize(HWND hwnd,WPARAM wparam,LPARAM lparam){
    return 0;
}

static LRESULT WmUser(HWND hwnd){
    POINT	pt;
    TCHAR	s[300];
    HDC		dc;

    if(rinfo.key){
	pt=points[rinfo.key%4];
	dc=GetDC(hwnd);
	wsprintf(s,"%d, %d",pt.x,pt.y);
	TextOut(dc,32,2,s,strlen(s));
	ReleaseDC(hwnd,dc);
    }
    return 0;
}

static LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam){
    switch(message){
    case WM_PAINT:
	return WmPaint(hwnd,wparam,lparam);
    case WM_SIZE:
	return WmSize(hwnd,wparam,lparam);
    case WM_USER:
	return WmUser(hwnd);
    default:
	return DefWindowProc(hwnd,message,wparam,lparam);
    }
    return 0;
}

//-----------------------------------------------------------------------------

HWND CreateKPad(HINSTANCE hinst,HWND hframe,U32 id){
    return CreateWindowEx(WS_EX_CLIENTEDGE,wclass,NULL,WS_CHILD|WS_BORDER,
	0,0,0,0,hframe,(HMENU)id,hinst,NULL);
}

ATOM InitKPad(HINSTANCE hins){
    WNDCLASS	c;

    c.style		=CS_HREDRAW|CS_VREDRAW;
    c.lpfnWndProc	=(WNDPROC)WndProc;
    c.cbClsExtra	=0;
    c.cbWndExtra	=0;
    c.hInstance		=hins;
    c.hIcon		=NULL;
    c.hCursor		=defcursor;
    c.hbrBackground	=(HBRUSH)(COLOR_WINDOW+1);
    c.lpszMenuName	=NULL;
    c.lpszClassName	=wclass;
    return RegisterClass(&c);
}
